#include <rtthread.h>
#include <rtdevice.h>

#include "button.h"
#include "beep.h"

rt_sem_t sw_1_sem;
rt_sem_t sw_2_sem;


void ist_sw_scan(void *param)
{
    int8_t temp = rt_pin_read(SW_1_IO);
    // rt_kprintf("sw1: %d\n", temp);
    if(temp == 0)
    {
        // rt_kprintf("sw1: %d\n", temp);
        rt_sem_release(sw_1_sem);
        ist_beep_on(1, 100);
    }

    temp = rt_pin_read(SW_2_IO);
        // rt_kprintf("sw2: %d\n",temp);
    if(temp == 0)
    {
        rt_sem_release(sw_2_sem);
        ist_beep_on(1, 100);
    }
}

void ist_sw_init(void)
{
    rt_pin_mode(SW_1_IO, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(SW_2_IO, PIN_MODE_INPUT_PULLUP);

    sw_1_sem = rt_sem_create("sw1_sem", 0, RT_IPC_FLAG_FIFO);
    sw_2_sem = rt_sem_create("sw2_sem", 0, RT_IPC_FLAG_FIFO);

    rt_timer_t timer = rt_timer_create("sw_scan", ist_sw_scan, RT_NULL, 10, RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
    rt_timer_start(timer);
}


INIT_COMPONENT_EXPORT(ist_sw_init);





